package fr.jade.fraclite.util;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.AttributeController;
import org.objectweb.fractal.api.control.NameController;
import org.objectweb.fractal.api.control.SuperController;

import fr.jade.fraclite.api.control.GenericAttributeController;

/**
 * Provides static methods to access extended interfaces of Fractal components.
 */
public class FracLite {

  /**
   * Returns the {@link AttributeController} interface of the given component.
   * 
   * @param component
   *          a component.
   * @return the {@link AttributeController} interface of the given component.
   * @throws NoSuchInterfaceException
   *           if there is no such interface.
   */

  public static GenericAttributeController getAttributeController(final Component component) throws NoSuchInterfaceException {
    return (GenericAttributeController) component.getFcInterface("generic-attribute-controller");
  }

  public static void toString (
      final Component component,
      final StringBuffer buf)
    {
      try {
        NameController nc =
          (NameController)component.getFcInterface("name-controller");
        try {
          SuperController sc =
            (SuperController)component.getFcInterface("super-controller");
          Component[] superComponents = sc.getFcSuperComponents();
          if (superComponents.length > 0) {
            toString(superComponents[0], buf);
          }
        } catch (NoSuchInterfaceException ignored) {
        }
        buf.append('/');
        buf.append(nc.getFcName());
      } catch (NoSuchInterfaceException e) {
        buf.append(component.toString());
        return;
      }
    }
}
